package com.qixiucoding.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.util.ResourceUtils;

import java.io.*;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author longhj
 * @Date 2021/11/20 星期六 22:53
 */

@Slf4j
public class JSONHelper {


    public static void main(String[] args) {
        // String s = ResolveJsonFileToString("config.json5");
        // System.out.println("sss=" + s);
        //
        // Map map = (Map) ResolveJsonFileToObject("config.json");
        // System.out.println("name=" + map.get("name"));

        try {
            File resFile = JsonUtil.getResFile("config.json5");
            BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(resFile));
            Map<String, String> o = JSONObject.parseObject(inputStream, HashMap.class, Feature.AllowComment);
            System.out.println(JSON.toJSONString(o));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    /**
     * 将文件流转为json对象，文件存放路径与配置文件路径规范一致
     *
     * @param
     * @return
     * @throws
     */
    public static Object ResolveJsonFileToObject(String filename) {
        String str = ResolveJsonFileToString(filename);
        JSONObject jo = JSONObject.parseObject(str);
        return jo;
    }


    /**
     * 通过文件名获取获取json格式字符串，
     *
     * @param filename 文件存放路径与配置文件路径规范一致
     * @return ResolveJsonFileToString
     * @throws
     */
    public static String ResolveJsonFileToString(String filename) {

        BufferedReader br = null;
        String result = null;
        try {

//            br = new BufferedReader(new InputStreamReader(getInputStream(path)));
            br = new BufferedReader(new InputStreamReader(getResFileStream(filename), "UTF-8"));
            StringBuffer message = new StringBuffer();
            String line = null;
            while ((line = br.readLine()) != null) {
                message.append(line);
            }
            if (br != null) {
                br.close();
            }
            String defaultString = message.toString();
            result = defaultString.replace("\r\n", "").replaceAll(" +", "");
            log.info("result={}", result);

        } catch (IOException e) {
            try {
                ClassLoader classloader = Thread.currentThread().getContextClassLoader();
                InputStream in = classloader.getResourceAsStream(filename);
                br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
                StringBuffer message = new StringBuffer();
                String line = null;
                while ((line = br.readLine()) != null) {
                    message.append(line);
                    message.append("\n");
                }
                if (br != null) {
                    br.close();
                }
                if (in != null) {
                    in.close();
                }
                String defaultString = message.toString();
                // result = defaultString.replace("\r\n", "").replaceAll(" +", "");
                result = defaultString;
                log.debug("for jar result={}", result);
            } catch (Exception e1) {
                e1.printStackTrace();
            }

        }

        return result;
    }


    private static File getResFile(String filename) throws FileNotFoundException {
        File file = new File(filename);
        if (!file.exists()) { // 如果同级目录没有，则去config下面找
            log.debug("不在同级目录，进入config目录查找");
            file = new File("config/" + filename);
        }
        Resource resource = new FileSystemResource(file);
        if (!resource.exists()) { //config目录下还是找不到，那就直接用classpath下的
            log.debug("不在config目录，进入classpath目录查找");
            file = ResourceUtils.getFile("classpath:" + filename);
        }
        return file;
    }

    /**
     * 通过文件名获取classpath路径下的文件流
     *
     * @param
     * @return
     * @throws
     */
    private static FileInputStream getResFileStream(String filename) throws FileNotFoundException {
        FileInputStream fin = null;
        File file = getResFile(filename);
        log.info("getResFile path={}", file);
        fin = new FileInputStream(file);
        return fin;
    }

}

